/*
 * 文 件 名:  DispatchAction.java
 * 版    权:  Domee Technologies Co., Ltd. Copyright 1993-2013,  All rights reserved
 * 描    述:  <描述>
 * 修 改 人:  yanglong
 * 修改时间:  2013-11-10
 * 跟踪单号:  <需求跟踪单号>
 * 修改单号:  <需求修改单号>
 * 修改内容:  <修改内容>
 */
package com.hfut.jpoa.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hfut.jpoa.common.action.AbsAction;
import com.hfut.jpoa.common.dto.Pagination;
import com.hfut.jpoa.common.dto.ResultDto;
import com.hfut.jpoa.common.util.ConstantUtil;
import com.hfut.jpoa.dto.QuoteDTO;
import com.hfut.jpoa.model.Quote;
import com.hfut.jpoa.service.DispatchService;
import com.hfut.jpoa.service.QuoteService;
import com.hfut.jpoa.service.exception.QuoteException;

/**
 * <一句话功能简述> <功能详细描述>
 * 
 * @author yanglong
 * @version [1.0, 2013-11-10]
 * @since [产品/1.0]
 */
public class QuoteAction extends AbsAction<QuoteDTO>
{

	private static final Logger LOGGER = LoggerFactory
	        .getLogger(QuoteAction.class);

	private static final long serialVersionUID = -7819502638545092791L;

	private QuoteService quoteService;

	private Quote quote;

	private List<Quote> lstQuotes;

	/**
	 * 标志位：0-增加 1-修改 2-详情
	 */
	private String actionType;

	/**
	 * 包含分页接受前台变量
	 */
	private QuoteDTO quoteDTO;

	/**
	 * 分页结果集
	 */
	private ResultDto<Quote> resultDto;

	/**
	 * 总页数
	 */
	private int totalPage;

	/**
	 * 创建者,以后从session中取
	 */
	private String creator = "yl";

	/**
	 * 调度服务
	 */
	private DispatchService dispatchService;

	/**
	 * 加载调度中心分页列表
	 * 
	 * @return
	 * @throws CategoryException
	 */
	public String listInit() throws QuoteException
	{
		return "list_init";
	}

	/**
	 * 加载列表页显示数据
	 */
	private void initDate() throws QuoteException
	{
		QuoteDTO dto = new QuoteDTO();
		Pagination pagination = new Pagination(0);
		dto.setPagination(pagination);
		resultDto = quoteService.queryInfoForPage(dto);
		totalPage = resultDto.getPagination().getTotalPage();
	}

	/**
	 * 加载售后部分页列表
	 * 
	 * @return
	 * @throws CategoryException
	 */
	public String customerListInit() throws QuoteException
	{
		QuoteDTO dto = new QuoteDTO();
		Pagination pagination = new Pagination(0);
		dto.setPagination(pagination);
		resultDto = quoteService.queryInfoForPage(dto);
		totalPage = resultDto.getPagination().getTotalPage();
		return "dispatch_list_init";
	}

	/**
	 * 加载列表数据
	 * 
	 * @return
	 */
	public String list() throws QuoteException
	{

		HttpServletRequest request = ServletActionContext.getRequest();
		String currentPage = request.getParameter("currentPage");
		Pagination pagination = new Pagination(Integer.parseInt(currentPage));
		String pagesize = request.getParameter("pageSize");
		pagination.setPageSize(Integer.parseInt(pagesize));
		if (quoteDTO == null)
		{
			quoteDTO = new QuoteDTO();
		}
		quoteDTO.setPagination(pagination);
		resultDto = quoteService.queryInfoForPage(quoteDTO);

		totalPage = resultDto.getPagination().getTotalPage();

		return "list";
	}

	/**
	 * 增加页面初始化
	 */
	public String addInit() throws QuoteException
	{

		actionType = ConstantUtil.ACTIONTYPE_ADD;

		return "add_init";
	}

	/**
	 * 新增保存
	 * 
	 * @return
	 */
	public String add() throws QuoteException
	{
		quote.setStation(ConstantUtil.STATION_UNAUDITED);
		quoteService.save(quote);
		return "dispatch_list_init";
	}

	/**
	 * 修改页面初始化
	 */
	public String dispatchUpdateInit() throws QuoteException
	{

		actionType = ConstantUtil.ACTIONTYPE_UPDATE;
		Quote resultDispatch = quoteService.findInfo(quote);
		quote = resultDispatch;

		return "dispatch_add_init";
	}

	/**
	 * 回访页面初始化
	 * 
	 * @return
	 * @throws QuoteException
	 */
	public String dispatchVisitInit() throws QuoteException
	{
		actionType = ConstantUtil.ACTIONTYPE_VISIT;
		Quote resultDispatch = quoteService.findInfo(quote);
		quote = resultDispatch;

		return "dispatch_add_init";
	}

	/**
	 * 回访
	 * 
	 * @return
	 * @throws QuoteException
	 */
	public String dispatchVisit() throws QuoteException
	{
		try
		{
			quoteService.update(quote);
		} catch (QuoteException e)
		{
			LOGGER.error("dispatchVisit error：", e);
			throw new QuoteException("dispatchVisit error：", e);
		}
		return "dispatch_list_init";
	}

	/**
	 * 修改字典表
	 * 
	 * @return
	 * @throws QuoteException
	 */
	public String updateDispatch() throws QuoteException
	{

		// quoteService.updateDispatch(dispatch);
		return "dispatch_list_init";
	}

	/**
	 * 修改字典表状态
	 */
	public void updateState()
	{
		/*
		 * try { // quoteService.updateDispatch(dispatch); } catch
		 * (QuoteException e1) { try { failure("操作异常：" + e1.getMessage()); }
		 * catch (IOException e) { LOGGER.error("修改字典配置异常", e); } } try {
		 * success("成功操作！"); } catch (IOException e) { LOGGER.error("修改字典配置异常",
		 * e); }
		 */
	}

	/**
	 * 验证编码是否存在
	 * 
	 * @throws IOException
	 */
	public void codeValidator() throws IOException
	{
		/*
		 * boolean flag = false; try { flag =
		 * quoteService.codeValidator(dispatch); } catch (QuoteException e) {
		 * LOGGER.error("验证编码是否存在异常", e); failure("验证编码是否存在异常" +
		 * e.getMessage()); } if (flag) { failure("该编码已存在，请重新输入"); } else {
		 * success("输入正确，该编码可以使用"); }
		 */
	}

	public String getActionType()
	{
		return actionType;
	}

	public void setActionType(String actionType)
	{
		this.actionType = actionType;
	}

	public int getTotalPage()
	{
		return totalPage;
	}

	public void setTotalPage(int totalPage)
	{
		this.totalPage = totalPage;
	}

	public List<Quote> getLstQuotes()
	{
		return lstQuotes;
	}

	public void setLstQuotes(List<Quote> lstQuotes)
	{
		this.lstQuotes = lstQuotes;
	}

	public QuoteService getQuoteService()
	{
		return quoteService;
	}

	public void setQuoteService(QuoteService quoteService)
	{
		this.quoteService = quoteService;
	}

	public Quote getQuote()
	{
		return quote;
	}

	public void setQuote(Quote quote)
	{
		this.quote = quote;
	}

	public ResultDto<Quote> getResultDto()
	{
		return resultDto;
	}

	public void setResultDto(ResultDto<Quote> resultDto)
	{
		this.resultDto = resultDto;
	}

	public DispatchService getDispatchService()
	{
		return dispatchService;
	}

	public void setDispatchService(DispatchService dispatchService)
	{
		this.dispatchService = dispatchService;
	}

}
